<?php

class Campaign extends Model_Abstract
{
	public function getAllCampaigns($publisherId = false)
	{
		try
		{
			if(!$publisherId){
				$sql = 'select c.id as campaign_id, c.name as campaign_name, c.pixel, p.name as publisher_name, s.name as sub_name, o.name 
						as offer_name from campaigns as c left join publishers as p on c.pub_id = p.id left join subs
						as s on c.sub_id = s.id left join offers as o on c.o_id = o.id';
				$result = $this->_connection->query($sql);
			}else{
				$sql = 'select c.id as campaingn_id, c.name as campaign_name, c.pixel, p.name as publisher_name, s.name as sub_name, o.name 
						as offer_name from campaigns as c left join publishers as p on c.pub_id = p.id left join subs
						as s on c.sub_id = s.id left join offers as o on c.o_id = o.id where c.pub_id=?';
				$result = $this->_connection->query($sql, array($publisherId));
			}
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getAllCampaignsRelatedToOffer($offerId){
		try
		{
			$sql = 'select id from campaigns where o_id=?';
			$result = $this->_connection->query($sql, array($offerId));
			if($result){
				$relatedCampaign = array();
				foreach ($result as $campaign){
					$relatedCampaign[] = $campaign['id'];
				}
				return $relatedCampaign;
			}
			return null;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getAllCampaignsByOffer(){
		try
		{
			$sql = 'select c.id, c.name as camp_name, p.name as pub_name, s.name as sub_name from campaigns 
						as c left join publishers as p on c.pub_id=p.id left join subs as s on c.sub_id=s.id ';
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function add($name, $pubId, $subId, $offerId, $pixel, $pixel_type)
	{
		try
		{
			$sql = 'insert into campaigns set name=?, pub_id=?, sub_id=?, o_id=?, pixel_type=?, pixel=?, created=?';
			$this->_connection->execute($sql, array($name, $pubId, $subId, $offerId, $pixel_type,$pixel, time()));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getCampaignById($camid)
	{
		try
		{
			$sql = 'select * from campaigns where id=? limit 1';
			$result = $this->_connection->query($sql, array($camid));
			if (count($result) == 1)
			{
				return $result[0];
			}
			else
			{
				return false;
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function search($name, $publisherId='', $subId='', $offerId=''){
		try{
				$sql = 'select c.id as campaign_id, c.name as campaign_name, c.pixel, p.name as publisher_name, s.name as sub_name, o.name 
						as offer_name from campaigns as c left join publishers as p on c.pub_id = p.id left join subs
						as s on c.sub_id = s.id left join offers as o on c.o_id = o.id where c.name like ? ';
				$vars = array("%" . trim($name). "%");
				$conditions = array();
				if('' != $publisherId){
					$conditions['pub_id'] = $publisherId;
				}
				if('' != $subId){
					$conditions['sub_id'] = $subId;
				}
				if('' != $offerId){
					$conditions['o_id'] = $offerId;
				}
				if(count($conditions) > 0){
					foreach ($conditions as $k=>$v){
						$sql .= "and c.$k=?";
						array_push($vars, $v);
					}
				}
				return $this->_connection->query($sql, $vars);
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
	}
	
	public function changeRelatedOffer($offerId, $campainId){
		try
		{
			$sql = "update campaigns set o_id=? where id=?";
			$this->_connection->execute($sql, array($offerId, $campainId));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	
}
?>